<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.SortedSet"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Iterator"%>
<%@page import="model.Room"%>
<%@page import="model.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rooms</title>
</head>
<body>
<a href="/hmg/IndexServlet">Home</a>
<br/>
<%
SortedSet<Room> r = (TreeSet<Room>)request.getAttribute("rooms");
if(r != null) {
  if(r.isEmpty()){
    %>
    No rooms where found!
    <%
  } else {
	  Iterator<Room> i = r.iterator();
	  %>
	  <h2>List of rooms in the database:</h2>
	  <table id="room_list">
	  	<thead>
	  		<tr>
	  			<th>Name</th>
	  			<th>Question</th>
	  			<th>Answer A</th>
	  			<th>Answer B</th>
	  			<th>Users/Max Users</th>
	  			<%
	  			User u = null;
	  			if(request.getSession()!=null && (u = (User)request.getSession().getAttribute("loggeduser"))!=null){
	  				%> <th>Enter room!</th> <%
	  			}
	  			%>
	  		</tr>
	  	</thead>
	  <%
	  while(i.hasNext()){
	    Room next = i.next();
	    %>
	    <tr>
	    	<td><%= next.getName() %></td>
	    	<td><%= next.getQuestion().getQuestion() %></td>
	    	<td><%= next.getQuestion().getAnswerA() %></td>
	    	<td><%= next.getQuestion().getAnswerB() %></td>
	    	<td><%= next.getQUsers() + "/" + next.getMaxUsers() %></td>
	    	<%
	    	if(request.getSession()!=null && (u = (User)request.getSession().getAttribute("loggeduser"))!=null){
  			%>
	    	<td>
	    		<form method="POST" action="RoomServlet">
	    			<input type="hidden" value="<%= next.getName() %>" id="room" name="room"/>
	    			<input type="submit" name="enterroombtn" id="enterroombtn" value="Enter!">
	    		</form>
	    	</td>
	    	<%} %>
	    </tr>
	    <%
	  }
	  %>
	  </table>
	  <%
  }
} else {
  %>
  	<h2>No rooms where found!</h2>
  <%
}
%>
</body>
</html>